var payObj = {
    lTeamId: "",
    lGroupBuyId: "",
    sUserId: "",
    flage: 1,
    init: function() {
        this.lTeamId = publicObj.getUrlData("lTeamId");
        this.sUserId = $.cookie('userid');
        this.getGoodsDetail();
        this.hidePtTip();
        this.pay();
    },
    getGoodsDetail: function() {
        var me = this;
        var url = magic_config.urlPrefix + 'GroupbuyService/getallgroupbuyinfo';
        if (me.userid == null) {
            sUserIdStr = "";
        } else {
            sUserIdStr = me.userid;
        }
        var data = {
            lTeamId: me.lTeamId,
            sUserId: sUserIdStr
        }
        $.ajax({
            url: url,
            type: 'get',
            data: data,
            async: false,
            dataType: 'json',
            success: function(res) {
                var nCode = res.return.nCode;
                if (nCode === 0) {
                    me.splitDetail(res.response.cdoData);
                } else {
                    console.log("数据请求失败，后到前");
                }
            },
            error: function(res) {
                console.log("数据请求失败，前到后");
            }
        });
    },
    splitDetail: function(goodsDetail) {
        console.log(goodsDetail);
        this.lGroupBuyId = goodsDetail.lGroupBuyId;
        //商品名字
        $(".goodsname").html(goodsDetail.sName);
        //商品价格
        $(".goodsprc").html('<span>拼团价&nbsp&nbsp&nbsp&nbsp¥&nbsp</span>' + goodsDetail.nGroupPrice);
        $(".paytip").html('应付款：<b>￥</b><span>' + goodsDetail.nGroupPrice + '</span>');
        $(".goodsoprc").html("¥" + goodsDetail.nMainPrice);
        $(".groupnum").html('<span>' + goodsDetail.nCountPerGroup + '</span>人参与');
        //商品图片
        $(".goodsimg").html('<img src="' + goodsDetail.sHeadPicture + '" alt="" />');
        $(".pttip2").html('拼团成功后，获得拼团券，快去<br><span>' + goodsDetail.sCorpName + '【扫码购】使用</span>');
    },
    //关闭提示弹层
    hidePtTip: function() {
        var me = this;
        $(".pttipbtn").bind("click", function() {
            $(".layerbox").hide();
            $(".pt_tip_box").hide();
            $.cookie("tipfalge", "true", {　　
                path: "/",
                expires: 365
            })
            console.log("调用支付接口");
            if (me.flage == 1) {
                me.flage = 2;
                wxPayNow();
            }
        })
        $(".n_l_dele").bind("click", function() {
            me.flage = 1;
            $(".normallogin").hide();
            $(".layerbox").hide();
        })
    },
    //点击确认下单
    pay: function() {
        var me = this;
        $(".paybtn").bind("click", function() {
            if ($.cookie('tipfalge') == null) {
                $(".layerbox").show();
                $(".pt_tip_box").show();
            } else {
                console.log("调用支付接口");
                if (me.flage == 1) {
                    me.flage = 2;
                    wxPayNow();
                }
            }
        })
    }
};
payObj.init();

function wxPayNow() {
    $.ajax({
        url: magic_config.urlPrefix + 'WxOpenidService/getjsapiconfig',
        data: {
            url: window.location.href,
            storeId: "S4001"
        },
        type: 'post',
        success: function(res) {
            console.log(res);
            wx.config({
                debug: false,
                appId: res.response.appId, //res.response.appId, // 必填，公众号的唯 
                nonceStr: res.response.nonceStr, // 必填，生成签名的随机串
                signature: res.response.signature, // 必填，签名，见附录1
                timestamp: res.response.timestamp, // 必填，生成签名的时间戳
                jsApiList: ['chooseWXPay']
            });
        },
        fail: function(res) {
            console.log(res);
        }
    });
    var _link = window.location.href;
    wx.ready(function() {
        var dataObj = {
            sUserId: $.cookie('userid'),
            lTeamId: payObj.lTeamId,
            lGroupBuyId: payObj.lGroupBuyId
        }
        console.log(dataObj);
        var url = magic_config.urlPrefix + 'GroupbuyService/getpublicnumpaysign';
        $.ajax({
            url: url,
            type: 'post',
            data: dataObj,
            async: false,
            dataType: 'json',
            success: function(res) {
                var nCode = res.return.nCode;
                if (nCode === 0) {
                    var wsPayObj = res.response.wxPayResult;
                    wx.chooseWXPay({
                        timestamp: wsPayObj.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
                        nonceStr: wsPayObj.nonceStr, // 支付签名随机串，不长于 32 位
                        package: wsPayObj.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
                        signType: wsPayObj.signType, // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
                        paySign: wsPayObj.paySign, // 支付签名
                        success: function(res) {
                            // 支付成功后的回调函数   
                            payObj.flage = 1;
                            statusCheckObj.init();
                        },
                        fail: function(res) {
                            console.log(res);
                            payObj.flage = 1;
                        },
                        complete: function() {
                            payObj.flage = 1;
                        }
                    });
                } else {
                    console.log("数据请求失败，后到前");
                    console.log(res);
                    if (res.return.nCode == 2) {
                        $(".layerbox").show();
                        $(".normallogin").show();
                         payObj.flage = 1;
                    } else {
                         payObj.flage = 1;
                        alert(res.return.strText)
                    }
                }
            },
            error: function(res) {
                console.log("数据请求失败，前到后");
            }
        });

    });
}